<%@ page import="entity.AdminEnity" %>
<%@ page import="dao.AdminDao" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
    request.setCharacterEncoding("UTF-8");
    AdminEnity adminUser = null;
    AdminDao adminDao = new AdminDao();
    adminUser = adminDao.getOneAdminUser();
    session.setAttribute("adminUser", adminUser);
%>
<!DOCTYPE html>
<html>
<head>
    <title>修改管理员信息</title>
    <script src="../static/js/bootstrap.bundle.min.js"></script>
    <script src="../static/js/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <link rel="stylesheet" href="../static/css/bootstrap.min.css">
    <script>
        function updateAdmin() {
            var id = document.getElementById('staffId').value;
            var name = document.getElementById('staffUsername').value;
            var password = document.getElementById('staffName').value;
            var xhr = new XMLHttpRequest();
            xhr.open('POST', '/updateAdmin', true);
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    if (xhr.responseText === 'true') {
                        showSuccess('SUCCESS！', '修改用户信息成功');
                    } else {
                        showError('ERROR！', '修改用户信息失败');
                    }
                }
            };
            var data = 'id=' + id + '&name=' + name + '&password=' + password;
            xhr.send(data);
        }
        function showSuccess(title, text = '', timer = 1000) {
            Swal.fire({
                icon:'success',
                title: title,
                text: text,
                timer: timer,
                showConfirmButton: false,
                toast: true, // 可选：设置为 Toast 模式（更轻量）
                position: 'top-center', // 可选：位置（右上角）
            });
        }

        function showError(title, text = '', timer = 1000) {
            Swal.fire({
                icon: 'error',
                title: title,
                text: text,
                timer: timer || undefined, // 0 时不设置 timer
                showConfirmButton:!!timer, // 如果 timer=0，显示确认按钮
            });
        }
    </script>
</head>
<body>
<div class="container" style="margin:30px auto;border-radius: 8px;height: 700px;align-items: center;display:flex;justify-content: center">
    <form>
        <div class="mb-3">
            <label for="staffId" class="form-label">id</label>
            <input type="text" class="form-control width" id="staffId" readonly value="<%=adminUser.getId()  %>" name="id">
        </div>
        <div class="mb-3">
            <label for="staffUsername" class="form-label">姓名</label>
            <input type="text" class="form-control width" id="staffUsername" value="<%=adminUser.getName()%>" name="name">
        </div>
        <div class="mb-3">
            <label for="staffName" class="form-label">密码</label>
            <input type="text" class="form-control width" id="staffName" value="<%=adminUser.getPassword()%>" name="password">
        </div>
        <button type="button" class="btn btn-primary width" style="margin-top:30px" onclick="updateAdmin()">提交修改</button>
    </form>
</div>
</body>
</html>